package com.agritech.smartfarming.model;

import com.agritech.smartfarming.strategy.StandardPathStrategy;

/**
 * 拖拉机类，继承自农机设备基类
 */
public class Tractor extends AgriculturalMachine {
    private double enginePower; // 发动机功率 (kW)
    private double towingCapacity; // 牵引能力 (kg)
    
    public Tractor(String id, String name) {
        super(id, name, "拖拉机");
        this.enginePower = 120.0;
        this.towingCapacity = 5000.0;
        // 默认使用标准路径规划策略
        this.pathStrategy = new StandardPathStrategy();
    }
    
    @Override
    public void performTask() {
        if (status == MachineStatus.WORKING) {
            System.out.println(name + " 正在执行耕地任务，发动机功率: " + enginePower + "kW");
        } else {
            System.out.println(name + " 当前不在工作状态，无法执行任务");
        }
    }
    
    @Override
    public void update(EnvironmentData environmentData) {
        System.out.println(name + " 接收到环境数据更新");
        
        // 根据环境数据调整作业参数
        if (environmentData.getSoilMoisture() > 80) {
            System.out.println("⚠️ " + name + " 检测到土壤湿度过高 (" + environmentData.getSoilMoisture() + 
                "%)，调整耕地深度，避免陷入泥泞");
        }
        
        if (environmentData.getWindSpeed() > 10) {
            System.out.println("⚠️ " + name + " 检测到风速过大 (" + environmentData.getWindSpeed() + 
                "m/s)，降低行驶速度以确保安全");
        }
        
        // 更新位置信息
        if (environmentData.getPosition() != null) {
            double distance = calculateDistance(this.position, environmentData.getPosition());
            if (distance < 100) {  // 如果环境数据采集点在农机100米范围内
                System.out.println(name + " 更新当前区域环境参数");
            }
        }
    }
    
    /**
     * 计算两点之间的距离
     */
    private double calculateDistance(Position p1, Position p2) {
        double latDiff = p2.getLatitude() - p1.getLatitude();
        double longDiff = p2.getLongitude() - p1.getLongitude();
        return Math.sqrt(latDiff * latDiff + longDiff * longDiff) * 111000;
    }
    
    /**
     * 获取拖拉机特有信息
     */
    public String getTractorInfo() {
        return String.format("发动机功率: %.1f kW, 牵引能力: %.1f kg", enginePower, towingCapacity);
    }
} 